package clientLogin;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class ResetPwd
 */
public class ResetPwdServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ResetPwdServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		HttpSession session = request.getSession();
		JDBCClientDAO DAO = new JDBCClientDAO();
		int clientID = Integer.parseInt(session.getAttribute("clientID").toString());
		Client client = new Client();
		client = DAO.getByID(clientID);
		
		String pwd = request.getParameter("Pwd");
		String newPwd = request.getParameter("NewPwd");
			
		if (pwd.equals(newPwd)){
			if(pwd.isEmpty()){
				String nextHtml = "/NullPwd.html";
				RequestDispatcher dispatcher = getServletContext()
						.getRequestDispatcher(nextHtml);
				dispatcher.forward(request, response);
				return;
			} else {
				DAO.updatePwd(client, newPwd);
				String nextHtml = "/RetSetSus.html";
				RequestDispatcher dispatcher = getServletContext()
						.getRequestDispatcher(nextHtml);
				dispatcher.forward(request, response);
				return;
			}
		} else {
			String nextHtml = "/NoSamePwd.html";
			RequestDispatcher dispatcher = getServletContext()
					.getRequestDispatcher(nextHtml);
			dispatcher.forward(request, response);
			return;
		}
	}

}
